import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { LoginUsers, Users } from './data/user';
import { Cards } from './data/card';
let _Users = Users;
let _Cards = Cards;

export default {
  /**
   * mock bootstrap
   */
  bootstrap() {
    let mock = new MockAdapter(axios);

    // mock success request
    mock.onGet('/success').reply(200, {
      msg: 'success'
    });

    // mock error request
    mock.onGet('/error').reply(500, {
      msg: 'failure'
    });

    //登录
    mock.onPost('/api/user/login').reply(config => {
      let {username, password} = JSON.parse(config.data);
      return new Promise((resolve, reject) => {
        let data = null;
        setTimeout(() => {
          let hasUser = LoginUsers.some(u => {
            if (u.username === username && u.password === password) {
              data = JSON.parse(JSON.stringify(u));
              data.password = undefined;
              return true;
            }
          });
		  data={"userid":1,"userlevel":"0"};
          if (hasUser) {
            resolve([200, { code: 0, msg: '请求成功', data }]);
          } else {
            resolve([200, { code: 4001, msg: '账号或密码错误' }]); 
          }
        }, 1000);
      });
    });
	
	//退出登录
  mock.onGet('/api/user/logout').reply(200, { code: 0, msg: ''});
	//查询用户的详细信息
  mock.onGet('/api/user/detail').reply(200, { code: 0, msg: '',data:{"username":"admin","userlevel":"0","contactMan":"赵子龙","contactMobile":"13803831221","regTime":"2017-04-08 14:14:29.0","skill":"各种疑难杂症","f_contactMobile":null}});	
	
  mock.onGet('/api/user/showself').reply(200, { code: 0, msg: '',data:{"id":"admin","sex":1,"age":"45","username":"赵子龙","contactMobile":"13803831221","college":"北京大学医学院","major":"临床医学","skill":"专治各种疑难杂症"}});	
	
  //修改用户密码
	mock.onGet('/api/user/editpwd').reply(config => {
      let {username, password} = JSON.parse(config.data);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve([200, { code: 0, msg: '请求成功'}]);
        }, 1000);
      });
    });
	

	
	//查询用户的卡报表统计信息
    mock.onGet('/api/report/cardstat').reply(config => {
	  /*
      let { name, addr, age, birth, sex } = config.params;
	  */
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 0,
            msg: '请求成功',
			data:{"activeCardSum":4120,"allCardSum":5008,"stopCardSum":108,"normalCardSum":1256}
          }]);
        }, 500);
      });
    });
	
	
	

    //获取用户列表
    mock.onGet('/user/list').reply(config => {
      let {name} = config.params;
      let mockUsers = _Users.filter(user => {
        if (name && user.name.indexOf(name) == -1) return false;
        return true;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            users: mockUsers
          }]);
        }, 1000);
      });
    });



    //获取用户列表（分页）
    mock.onGet('/user/listpage').reply(config => {
      let {page, name} = config.params;
      let mockUsers = _Users.filter(user => {
        if (name && user.name.indexOf(name) == -1) return false;
        return true;
      });
      let total = mockUsers.length;
      mockUsers = mockUsers.filter((u, index) => index < 20 * page && index >= 20 * (page - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            total: total,
            users: mockUsers
          }]);
        }, 1000);
      });
    });
  
  mock.onGet('/api/user/det').reply(200, { code: 0, msg: '',data:{"name":"asd",
                "sex": "0",
                "age":"65",
                "mobile": "13803821224",
                "date": "2017-04-08",
                "morbidity": "感冒",}});
   mock.onGet('/api/user/not_receive').reply(config => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 0,
            msg: '请求成功',
			data:{
        "pageNum": 1,
        "pageSize": 10,
        "size": 10,
        "orderBy": null,
        "startRow": 1,
        "endRow": 10,
        "total": 100000,
        "pages": 10000,
        "list": [
            {
                "userid": 1,
                "name":"asd",
                "sex": "1",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "感冒,头痛，发烧，恶心，食欲不振，浑身无力",
            },
            {
                 "userid": 2,
                "name":"weq",
                "sex": "0",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "感冒",
            },
            {
                "userid":3,
                "name":"ghfd",
                "sex": "1",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "发烧",
            },
            {
                "userid": 4,
                "name":"yjt",
                "sex": "0",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "感冒",
            },
            {
                 "userid": 5,
                "name":"uiiu",
                "sex": "0",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "感冒",
            },
            {
                 "userid": 6,
                "name":"udfdf",
                "sex": "0",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "感冒",
            },
            {
                "userid": 7,
                "name":"vc",
                "sex": "1",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "感冒",
            },
            {
                "userid": 8,
                "name":"td",
                "sex": "0",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "感冒",
            },
            {
                 "userid": 9,
                "name":"bgfd",
                "sex": "1",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "感冒",
            },
            {
                "userid": 10,
                "name":"xdr",
                "sex": "0",
                "age":"65",
                "mobile": "13803821224",
                "n_time": "2017-04-08",
                "morbidity": "感冒",
            }
        ],
        "firstPage": 1,
        "prePage": 0,
        "nextPage": 2,
        "lastPage": 8,
        "isFirstPage": true,
        "isLastPage": false,
        "hasPreviousPage": false,
        "hasNextPage": true,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8
        ]
    }




          }]);
        }, 500);
      });
    });

    mock.onGet('/api/user/likeusers').reply(config => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 0,
            msg: '请求成功',
			data:{
        "pageNum": 1,
        "pageSize": 10,
        "size": 10,
        "orderBy": null,
        "startRow": 1,
        "endRow": 10,
        "total": 100000,
        "pages": 10000,
        "list": [
            {
                "userid": 1,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "0",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538000"
            },
            {
                "userid": 2,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "0",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538"
            },
            {
                "userid": 3,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "0",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538"
            },
            {
                "userid": 4,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "1",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08 14:14:29.0",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538"
            },
            {
                "userid": 5,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "2",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08 14:14:29.0",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538"
            },
            {
                "userid": 6,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "0",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08 14:14:29.0",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538"
            },
            {
                "userid": 7,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "1",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08 14:14:29.0",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538"
            },
            {
                "userid": 8,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "0",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08 14:14:29.0",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538"
            },
            {
                "userid": 9,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "0",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08 14:14:29.0",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538"
            },
            {
                "userid": 10,
                "servicerName":"郑州二七区服务商",
                "username": "admin",
                "password":"123456",
                "userlevel": "0",
                "contactMan": "赵子龙",
                "contactMobile": "13803821224",
                "regTime": "2017-04-08 14:14:29.0",
                "f_contactMan": "王书麒",
                "f_contactMobile": "15824385514",
                "cardnum":"1538"
            }
        ],
        "firstPage": 1,
        "prePage": 0,
        "nextPage": 2,
        "lastPage": 8,
        "isFirstPage": true,
        "isLastPage": false,
        "hasPreviousPage": false,
        "hasNextPage": true,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8
        ]
    }




          }]);
        }, 500);
      });
    });





    //删除用户
    mock.onGet('/api/user/remove').reply(config => {
      let { id } = config.params;
      _Users = _Users.filter(u => u.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '删除成功'
          }]);
        }, 500);
      });
    });

    //批量删除用户
    mock.onGet('/user/batchremove').reply(config => {
      let { ids } = config.params;
      ids = ids.split(',');
      _Users = _Users.filter(u => !ids.includes(u.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '删除成功'
          }]);
        }, 500);
      });
    });

    //编辑用户
    mock.onPost('/api/user/edit').reply(config => {
      /*
      let { id, name, addr, age, birth, sex } = config.params;
      _Users.some(u => {
        if (u.id === id) {
          u.name = name;
          u.addr = addr;
          u.age = age;
          u.birth = birth;
          u.sex = sex;
          return true;
        }
      });
      */
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '编辑成功'
          }]);
        }, 500);
      });
    });

    //新增用户
    mock.onPost('/api/user/add').reply(config => {
      /*
      let { name, addr, age, birth, sex } = config.params;
      _Users.push({
        name: name,
        addr: addr,
        age: age,
        birth: birth,
        sex: sex
      });
      */
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '新增成功'
          }]);
        }, 500);
      });
    });


    //模糊查询匹配的如库管理列表
    mock.onGet('/api/cards/like').reply(config => {
      let {pageNum, pageSize, likeCardKey} = config.params;
      let mockCards = _Cards.filter(card => {
        if (likeCardKey && card.PACKAGENAME.indexOf(likeCardKey) == -1) return false;
        return true;
      });
      let total = mockCards.length;
      mockCards = mockCards.filter((u, index) => index < pageSize * pageNum && index >= pageSize * (pageNum - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 0,
            msg: '请求成功',
			      data:{
              total: total,
              list: mockCards
            }
          }]);
        }, 1000);
      });
    });

    //删除卡号
    mock.onGet('/api/cards/remove').reply(config => {
      let { id } = config.params;
      _Cards = _Cards.filter(u => u.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            code: 200,
            msg: '删除成功'
          }]);
        }, 500);
      });
    });
    


  }
};